﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 泛型_集合
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 关联性泛型集合
            #endregion
            #region Dictionary(哈希表)
            Dictionary<string, product> dictproduct = new Dictionary<string, product>();
            dictproduct.Add("t1", new product());
            dictproduct.Add("t2", new product());
            dictproduct.Add("t3", new product());
            Console.WriteLine(dictproduct.Count);
            //获取值
            product p;
            Console.WriteLine(dictproduct.TryGetValue("t1",out p));
            //获取键
            foreach(var key in dictproduct.Keys)
                Console.WriteLine("{0}\t",key);
            #endregion
            #region SortedDictionary(二叉树)
            SortedDictionary<string, product> sdictionary = new SortedDictionary<string, product>();
            sdictionary.Add("t1", new product());
            sdictionary.Add("t4", new product());
            sdictionary.Add("t3", new product());
            Console.WriteLine(sdictionary.Count);
            Console.WriteLine(sdictionary.ContainsKey("t10"));
            foreach (var key in sdictionary.Keys)
                Console.WriteLine("{0}\t", key);
            #endregion
            #region Sortlist（数组）
            SortedList<string, product> slist = new SortedList<string, product>();
            slist.Add("t1", new product());
            slist.Add("t4", new product());
            slist.Add("t3", new product());
            foreach (var key in sdictionary.Keys)
                Console.WriteLine("{0}\t", key);
            Console.WriteLine(slist.ContainsValue(null));
            #endregion
            #region 非关联性泛型集合
            #region List(数组)
            List<product> Lproducts = new List<product>();
            Lproducts.Add(new product());
            Console.WriteLine(Lproducts[0].GetHashCode());
            #endregion
            #region Linklist(双向链表)
            LinkedList<product> llproducts = new LinkedList<product>();
            llproducts.AddLast(new product("aa","类型"));
            llproducts.AddFirst(new product("bb", "类型"));
            foreach(var product in llproducts)
                Console.WriteLine(product.Name+product.TypeName);
            #endregion
            #region HashSet哈希表  注意唯一性，构造重构Equals和Hash方法
            HashSet<product> hsproducts = new HashSet<product>();
            hsproducts.Add(new product("aa", "类型"));
            hsproducts.Add(new product("bb", "类型"));
            hsproducts.Add(new product("aa", "类型"));
            hsproducts.Add(new product("dd", "类型"));
            foreach (var product in hsproducts)
                Console.WriteLine(product.Name + product.TypeName);
            #endregion
            #region SortedSet(二叉树)

            SortedSet<product> ssproducts = new SortedSet<product>();
            ssproducts.Add(new product("aa", "类型"));
            ssproducts.Add(new product("bb", "类型"));
            ssproducts.Add(new product("aa", "类型"));
            ssproducts.Add(new product("dd", "类型"));
            foreach (var product in hsproducts)
                Console.WriteLine(product.Name + product.TypeName);
            #endregion
            /******笔记******
            如果是Sort,则要注意比较的关系，通过：comparable实现，注意
                 return name.CompareTo(((product)obj).name);
                 如果是Set,要注意唯一性，通过重写Equals,和Hash实现
            */
            #region Stack(栈)
            Stack<product> sproducts = new Stack<product>();
            sproducts.Push(new product("t1", "类型"));
            sproducts.Push(new product("t1", "类型"));
            Console.WriteLine(sproducts.Peek().Name);
            Console.WriteLine(sproducts.Pop().Name);
            Console.WriteLine(sproducts.Peek().Name);
            #endregion 
            #region queue(队列)
            Queue<product> qproducts = new Queue<product>();
            qproducts.Enqueue(new product("t1", "类型"));
            qproducts.Enqueue(new product("t2", "类型"));
            Console.WriteLine(qproducts.Peek().Name);
            Console.WriteLine(qproducts.Dequeue().Name);
            Console.WriteLine(qproducts.Peek().Name);
            #endregion
            #endregion
            Console.ReadKey();
        }
    }
}
